package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface MenuDao {

    public Set<Menu> findByRoleId(Integer roleId);

    //添加菜单
    @Insert("insert into t_menu(name,linkUrl,path,priority,description,icon,parentMenuId,level) values (#{name},#{linkUrl},#{path},#{priority},#{description},#{icon},#{parentMenuId},#{level})")
    void add(Menu menu);

    //分页
    Page<Menu> selectByCondition(String queryString);

    //查询是否关联角色
    @Select("select count(*) from t_role_menu where role_id = #{roleId}")
    long findCountByRoleId(Integer roleId);

    //删除菜单
    @Update("delete from t_menu where id = #{id}")
    void del(Integer id);

    //回显菜单信息
    @Select("select * from t_menu where id = #{id}")
    Menu findById(Integer id);

    //编辑菜单信息
    void edit(Menu menu);

    //查询所有菜单
    @Select("select * from t_menu")
    List<Menu> findAll();


    //查询所有一级菜单
    @Select("select * from t_menu where level=1")
    List<Menu> queryAllFirstMenu();

    //查询所有子菜单
    @Select("select * from t_menu where level=2")
    List<Menu> queryChildrenMenu();

//    @Select("select * from t_menu where parentMenuId=#{parentId}")
    List<Menu> findChildrenListByParentId(Integer parentId);

    @Select("select COUNT(*) from t_menu where parentMenuId=#{parentId}")
    Integer hasChildren(Integer parentId);

    @Select("SELECT * \n" +
            "FROM \n" +
            "\tt_menu m,\n" +
            "\tt_role_menu rm,\n" +
            "\tt_user_role ur,\n" +
            "\tt_user u\n" +
            "\t\n" +
            "WHERE \n" +
            "\tm.id=rm.menu_id \n" +
            "\tAND rm.role_id=ur.role_id \n" +
            "\tAND ur.user_id = u.id\n" +
            "\tAND u.id=(SELECT id FROM t_user WHERE username= #{usename}) ")
    List<Menu> findMenuByUsername(String username);

    @Select("select ur.role_id from t_user_role ur,t_user u where ur.user_id = u.id and u.id = (select id from t_user where username = #{username} )")
    List<Integer> findRoleIdList(String username);

    @Select("select ru.menu_id from t_role_menu ru where ru.role_id = #{roleId}")
    List<Integer> findMenuList(Integer roleId);

    @Select("select * from t_menu where id=#{menuId} and level = 1")
    Menu findFirstMenu(Integer menuId);

    List<Menu> findMenuOneByRoleId(Integer roleId);

    List<Menu> findMenuTwoByMenuOneId(Integer id);

    List<Menu> findMenuOneByRoleId(Integer roleId);

    List<Menu> findMenuTwoByMenuOneId(Integer id);
}
